package cn.hxzy.zy.office.controller;

import cn.hxzy.zy.office.entity.*;;
import cn.hxzy.zy.office.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class PublicController {
    @Autowired
    private UserService userService;


    @GetMapping("/main")
    public ModelAndView main() {
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }

    @GetMapping("/")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    @PostMapping("/userLogin")
    public String saveUser(String account, String password, Integer roleId, HttpSession session) {
        User user = userService.find(account);
        session.setAttribute("user", user);
        if (user != null) {
            if (password.equals(user.getPassword()) && roleId.equals(user.getRoleId())) {
                session.setAttribute("loginUser", user);
                if (user.getRoleId() == 0) {
                    return "redirect:/index";
                } else {
                    return "redirect:/userIndex";
                }
            }
        }
        session.setAttribute("info", "账号或密码错误，请重新登录！");
        return "redirect:/";
    }

    @GetMapping("/userIndex")
    public ModelAndView userIndex() {
        ModelAndView modelAndView = new ModelAndView("userIndex");
        return modelAndView;
    }

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }


    @Autowired
    private DeptService deptService;
    @Autowired
    private NationalService nationalService;
    @Autowired
    private EducationService educationService;
    @Autowired
    private DeptScheduleService deptScheduleService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserScheduleService userScheduleService;

    @GetMapping("/searchUser")
    public ModelAndView searchUser() {
        ModelAndView modelAndView = new ModelAndView("searchUser");
        List<User> list = userService.queryAll();
        modelAndView.addObject("users", list);
        return modelAndView;
    }

    @GetMapping("/createUser")
    public ModelAndView createUser1() {
        ModelAndView modelAndView = new ModelAndView("createUser");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        List<National> all = nationalService.queryAll();
        modelAndView.addObject("nationals", all);
        List<Education> list1 = educationService.queryAll();
        modelAndView.addObject("educations", list1);
        return modelAndView;
    }


    @ModelAttribute
    public void test(Map<String, Object> map) {
        User user = new User();
        Dept dept = new Dept();
        DeptSchedule deptSchedule = new DeptSchedule();
        deptSchedule.setStartDate(new Date());
        dept.setNumber("0");
        Project project = new Project();
        project.setId(1);
        user.setNumber("0");
        user.setRoleId(0);
        user.setProject(project);
        System.out.println("从数据库中获取一个对象: " + user);
        map.put("user", user);
        map.put("dept", dept);
    }

    /**
     * 添加员工
     *
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public String saveUser(User user) {
        user.setNumber(System.currentTimeMillis() + "");
        userService.insert(user);
        return "redirect:/searchUser";
    }

    /**
     * 员工编辑
     *
     * @param id
     * @return
     */
    @GetMapping("/updateUser/{id}")
    public ModelAndView updateUser(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateUser");
        modelAndView.addObject("user", userService.queryById(id));
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        List<National> all = nationalService.queryAll();
        modelAndView.addObject("nationals", all);
        List<Education> list1 = educationService.queryAll();
        modelAndView.addObject("educations", list1);
        return modelAndView;
    }

    @PostMapping("/editUser/{id}")
    public String editUser(User user) {
        user.setNumber(System.currentTimeMillis() + "");
        userService.update(user);
        return "redirect:/searchUser";
    }

    /**
     * 删除员工
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return "redirect:/searchUser";
    }

    /**
     * 添加部门
     *
     * @return
     */
    @GetMapping("/createDept")
    public ModelAndView createDept() {
        ModelAndView modelAndView = new ModelAndView("createDept");
        return modelAndView;
    }

    @PostMapping("/saveDept")
    public String saveDept(Dept dept) {
        dept.setNumber(System.currentTimeMillis() + "");
        deptService.insert(dept);
        return "redirect:/searchDept";
    }

    /**
     * 部门全部查询
     *
     * @return
     */
    @GetMapping("/searchDept")
    public ModelAndView searchDept() {
        ModelAndView modelAndView = new ModelAndView("searchDept");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        return modelAndView;
    }

    /**
     * 部门编辑
     *
     * @param id
     * @return
     */
    @GetMapping("/updateDept/{id}")
    public ModelAndView updateDept(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateDept");
        modelAndView.addObject("dept", deptService.queryById(id));
        return modelAndView;
    }

    @PostMapping("/editDept/{id}")
    public String updateDept(Dept dept) {
        dept.setNumber(System.currentTimeMillis() + "");
        deptService.update(dept);
        return "redirect:/searchDept";
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteDept/{id}")
    public String deleteDept(@PathVariable("id") Integer id) {
        deptService.deleteById(id);
        return "redirect:/searchDept";
    }

    /**
     * 添加日程
     *
     * @return
     */
    @GetMapping("/createDeptSchedule")
    public ModelAndView createDeptSchedule() {
        ModelAndView modelAndView = new ModelAndView("createDeptSchedule");
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        return modelAndView;
    }

    @PostMapping("/saveDeptSchedule")
    public String saveDeptSchedule(DeptSchedule deptSchedule) {
        deptScheduleService.insert(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }

    /**
     * 部门查询全部
     *
     * @return
     */
    @GetMapping("/searchDeptSchedule")
    public ModelAndView searchDeptSchedule() {
        ModelAndView modelAndView = new ModelAndView("searchDeptSchedule");
        List<DeptSchedule> list = deptScheduleService.queryAll();
        modelAndView.addObject("deptSchedules", list);
        return modelAndView;
    }

    /**
     * 日程编辑
     *
     * @param id
     * @return
     */
    @GetMapping("/updateDeptSchedule/{deptSchedule.id}")
    public ModelAndView updateDeptSchedule(@PathVariable("deptSchedule.id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateDeptSchedule");
        DeptSchedule deptSchedule = deptScheduleService.queryById(id);
        List<Dept> list = deptService.queryAll();
        modelAndView.addObject("depts", list);
        modelAndView.addObject("deptSchedule", deptSchedule);
        return modelAndView;
    }

    @PostMapping("/editDeptSchedule/{deptSchedule.id}")
    public String editDeptSchedule(@PathVariable("deptSchedule.id") Integer id, DeptSchedule deptSchedule) {
        deptSchedule.setId(id);
        deptScheduleService.update(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }

    /**
     * 删除日志
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteDeptSchedule/{id}")
    public String deleteDeptSchedule(@PathVariable("id") Integer id) {
        deptScheduleService.deleteById(id);
        return "redirect:/searchDeptSchedule";
    }

    /**
     * 员工档案
     *
     * @return
     */
    @GetMapping("/searchUserDoc")
    public ModelAndView searchUserDoc() {
        ModelAndView modelAndView = new ModelAndView("searchUserDoc");
        List<User> list = userService.queryAll();
        modelAndView.addObject("users", list);
        return modelAndView;
    }

    /**
     * 公告全部查询
     *
     * @return
     */
    @GetMapping("/searchNews")
    public ModelAndView searchNews() {
        ModelAndView modelAndView = new ModelAndView("searchNews");
        List<News> all = newsService.queryAll();
        modelAndView.addObject("news", all);
        return modelAndView;
    }

    /**
     * 发布公告
     *
     * @param news
     * @return
     */
    @PostMapping("/saveNews")
    public String saveNews(News news, HttpSession session) {
//        User user = new User();
//        user.setId(1);
//        news.setUser(user);
        Object longUser = session.getAttribute("LongUser");
        news.setUser((User) longUser);
        news.setCreateTime(new Date());
        newsService.insert(news);
        return "redirect:/searchNews";
    }

    @GetMapping("/createNews")
    public ModelAndView createNews() {
        ModelAndView modelAndView = new ModelAndView("createNews");
        return modelAndView;
    }

    /**
     * 公告编辑
     *
     * @param id
     * @return
     */
    @GetMapping("/updateNews/{id}")
    public ModelAndView updateNews(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateNews");
        News news = newsService.queryById(id);
        modelAndView.addObject("news", news);
        return modelAndView;
    }

    @PostMapping("/editNews/{id}")
    public String editNews(News news) {
        newsService.update(news);
        return "redirect:/searchNews";
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteNews/{id}")
    public String deleteNews(@PathVariable("id") Integer id) {
        newsService.deleteById(id);
        return "redirect:/searchNews";
    }

    /**
     * 查询全部考勤
     *
     * @return
     */
    @GetMapping("/searchAttendance")
    public ModelAndView searchAttendance() {
        ModelAndView modelAndView = new ModelAndView("searchAttendance");
        List<Attendance> list = attendanceService.queryAll();
        modelAndView.addObject("attendances", list);
        return modelAndView;
    }

    /**
     * 添加考勤
     *
     * @return
     */
    @GetMapping("/createAttendance")
    public ModelAndView createAttendance() {
        ModelAndView modelAndView = new ModelAndView("createAttendance");
        List<User> list = userService.queryAll();
        modelAndView.addObject("users", list);
        return modelAndView;
    }

    @PostMapping("/saveAttendance")
    public String saveAttendance(Attendance attendance) {
        attendanceService.insert(attendance);
        return "redirect:/searchAttendance";
    }

    /**
     * 员工薪水查询
     *
     * @return
     */
    @GetMapping("/searchUserWages")
    public ModelAndView searchUserWages() {
        ModelAndView modelAndView = new ModelAndView("searchUserWages");
        List<User> list = userService.queryAll();
        modelAndView.addObject("users", list);
        return modelAndView;
    }

    /**
     * 修改薪水
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUserWages/{id}")
    public String updateUserWages(User user) {
        userService.updateWages(user);
        return "redirect:/searchUserWages";
    }

    /**
     * 添加项目
     *
     * @return
     */
    @GetMapping("/createProject")
    public ModelAndView createProject() {
        ModelAndView modelAndView = new ModelAndView("createProject");
        List<Project> list = projectService.queryAll();
        modelAndView.addObject("projects", list);
        return modelAndView;
    }

    @PostMapping("/saveProject")
    public String saveProject(Project project) {
        projectService.insert(project);
        return "redirect:/searchProject";
    }

    /**
     * 查询项目
     *
     * @return
     */
    @GetMapping("/searchProject")
    public ModelAndView searchProject() {
        ModelAndView modelAndView = new ModelAndView("searchProject");
        List<Project> list = projectService.queryAll();
        modelAndView.addObject("projects", list);
        return modelAndView;
    }

    /**
     * 项目编辑
     *
     * @param id
     * @return
     */
    @GetMapping("/updateProject/{id}")
    public ModelAndView updateProject(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateProject");
        Project project = projectService.queryById(id);
        modelAndView.addObject("project", project);
        return modelAndView;
    }

    @PostMapping("/editProject/{id}")
    public String searchProject(Project project) {
        projectService.update(project);
        return "redirect:/searchProject";
    }

    /**
     * 项目删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteProject/{id}")
    public String deleteProject(@PathVariable("id") Integer id) {
        projectService.deleteById(id);
        return "redirect:/searchProject";
    }

    /**用户功能
     *
     * @return
     */
    /**
     * 日程全部查询
     *
     * @return
     */
    @GetMapping("/searchUserSchedule")
    public ModelAndView searchUserSchedule() {
        ModelAndView modelAndView = new ModelAndView("searchUserSchedule");
        List<UserSchedule> list = userScheduleService.queryAll();
        modelAndView.addObject("userSchedules", list);
        return modelAndView;
    }

    /**
     * 删除日程
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteUserSchedule/{id}")
    public String deleteUserSchedule(@PathVariable("id") Integer id) {
        userScheduleService.deleteById(id);
        return "redirect:/searchUserSchedule";
    }

    /**
     * 编辑日程
     *
     * @param id
     * @return
     */
    @GetMapping("/updateUserSchedule/{id}")
    public ModelAndView updateUserSchedule(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateUserSchedule");
        UserSchedule userSchedule = userScheduleService.queryById(id);
        modelAndView.addObject("userSchedule", userSchedule);
        return modelAndView;
    }

    //    @PostMapping("/editUserSchedule/{id}")
//    public String editUser(@PathVariable("id") Integer id, UserSchedule userSchedule) {
//        userSchedule.setId(id);
//
//        userScheduleService.update(userSchedule);
//        return "redirect:/searchUserSchedule";
//    }
    @PostMapping("/editUserSchedule/{id}")
    public String editUser(HttpSession session, UserSchedule userSchedule) {
        Object longUser = session.getAttribute("LongUser");
        userSchedule.setUser((User) longUser);
        userScheduleService.update(userSchedule);
        return "redirect:/searchUserSchedule";
    }

    /**
     * 日程添加
     *
     * @return
     */
    @GetMapping("/createUserSchedule")
    public ModelAndView createUserSchedule() {
        ModelAndView modelAndView = new ModelAndView("createUserSchedule");
        return modelAndView;
    }

    @PostMapping("/saveUserSchedule")
    public String saveUserSchedule(UserSchedule userSchedule, HttpSession session) {
        User user = (User) session.getAttribute("user");
        userSchedule.setUser(user);
        userScheduleService.insert(userSchedule);
        return "redirect:/searchUserSchedule";
    }

    /**
     * 我的文档
     *
     * @param session
     * @return
     */
    @GetMapping("/searchMyDoc")
    public ModelAndView searchMyDoc(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("searchMyDoc");
        Object user = session.getAttribute("loginUser");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    /**
     * 公告查询
     *
     * @return
     */
    @GetMapping("/searchNewsInfo")
    public ModelAndView searchNewsInfo() {
        ModelAndView modelAndView = new ModelAndView("searchNewsInfo");
        List<News> newsList = newsService.queryAll();
        modelAndView.addObject("news", newsList);
        return modelAndView;
    }
}



